package de.backtomedieval.buildings;

import de.backtomedieval.game.City;
import de.backtomedieval.game.Costs;
import de.backtomedieval.game.Earning;
import de.backtomedieval.units.AbstractUnit;

public abstract class AbstractBuilding
{
	
	private boolean active = true;
	private boolean builded = false;
	private int level = 0;
	private int healthpoints = 500;
	private int defense = 1;
	private int rue = 0; //rue = rounds until earn
	private Earning earn = null;
	private Costs costs = null;
	private int buildCountdown = 0;	
	private int lastFullBuildCountdown = 0;
	private int secondChance = 0;
	
	abstract int getDefenseBonus();
	abstract int getHealthpointsBonus();
	abstract int getIncomeBonus();
	public abstract int getImage();
	
	
	public boolean isActive() {
		return active;
	}
	public void setActive(boolean active, City city) {
		int[] activeDiff = {0,0,0,0,0};
		for (int i=0; i<activeDiff.length; i++) {
			int plus =0;
			int minus =0;
			if (earn!=null) plus =earn.getWare(level,i);
			if (costs!=null) minus = costs.getSupply(level,i);
			activeDiff[i]= plus - minus;
		}
		int[] updateWares = city.getUpdateWares();
		if (this.active == false && active == true) {
			for (int i=0; i<activeDiff.length; i++) {
				updateWares[i] = updateWares[i] +activeDiff[i];
			}
		} else if (this.active==true && active==false) {
			for (int i=0; i<activeDiff.length; i++) {
				updateWares[i] = updateWares[i] -activeDiff[i];
			}
		}
		city.setUpdateWares(updateWares);
		this.active = active;
	}
	public int getBuildCountdown() {
		return buildCountdown;
	}
	public void setBuildCountdown(int buildCountdown) {
		this.buildCountdown = buildCountdown;
	}
	public int getLastFullBuildCountdown() {
		return lastFullBuildCountdown;
	}
	public void setLastFullBuildCountdown(int lastFullBuildCountdown) {
		this.lastFullBuildCountdown = lastFullBuildCountdown;
	}
	public int getHealthpoints() {
		return healthpoints;
	}
	public void setHealthpoints(int healthpoints) {
		this.healthpoints = healthpoints;
	}
	
	AbstractBuilding(int healthpoints, int defense)
	{
		this.healthpoints = healthpoints + this.getHealthpointsBonus();
		this.defense = defense;
	}
	
	public void builded() {
		builded = true;
		levelup();
	}
	
	
	
	public void setBuilded(boolean builded) {
		this.builded = builded;
	}
	public void destroy() {
		builded = false;
	}
	
	public boolean isBuilded() {
		return builded;
	}
	
	public int getRue() {
		return rue;
	}
	public void setRue(int rue) {
		this.rue = rue;
	}
	public int getDefense() {
		return defense;
	}
	public void setDefense(int defense) {
		this.defense = defense;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
		if (level>0) {
			builded=true;
		} else {
			builded = false;
		}
	}
	public Earning getEarn() {
		return earn;
	}
	public void setEarn(Earning earn) {
		this.earn = earn;
	}
	public Costs getCosts() {
		return costs;
	}
	public void setCosts(Costs costs) {
		this.costs = costs;
	}
	
	
	public void levelup() {
		this.level = this.level + 1;
	}
	
	
	public int hurt(int damage) {
		if (!builded) return 0;
		if (healthpoints <= damage) {
			int lostHealthpoints = healthpoints;
			healthpoints = 0;
			this.level=0;
			this.builded=false;
			return lostHealthpoints;
		}
		healthpoints = healthpoints - damage;
		
		return damage;
	}
	
	/* deprecated.
	public void paySupply(City city) {
		if (!active) return;
		if (!builded) return;
		if (!city.canAffordSupply(this)) {
			setActive(false,city);
			return;
		}
		for (int i = 0; i<5;i++) {
			city.pay(i, this.costs.getSupply(this.level,i));
		}	
	}
	*/
	
	public String getName() {
		return this.getClass().getSimpleName();
	}
	
	public String getBuildPercent() {
		return ((lastFullBuildCountdown - buildCountdown )*100/lastFullBuildCountdown) + "%";
	}
}